//
//  JWNewsTool.m
//  见闻新闻
//
//  Created by brother on 16/3/17.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWNewsTool.h"
#define ID @"a_id"
#define name @"jx_name"
#define title @"jx_title"
#define appkey @"jx_appkey"
#define html @"jx_html"
#define pic  @"jx_pic"
#define t_auslese @"t_auslese"
#define a_ID @"jx_id"

@implementation JWNewsTool
static FMDatabase *_db;
+ (NSString *)getFilePath {
    //NSLog(@"%@", [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"Newsdb.sqlite3"]);
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"Newsdb.sqlite3"];
}

+ (FMDatabase *)shareDataBase {
    if (_db == nil) {
        _db = [FMDatabase databaseWithPath:[self getFilePath]];
        if (![_db open]) {
            return nil;
        }else {
            NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS t_abc(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, html TEXT, appkey TEXT,pic TEXT, title TEXT, jx_id TEXT)"];
            BOOL sussecc = [_db executeUpdate:sql];
            if (sussecc) {
                NSLog(@"创建成功");
            }else {
                NSLog(@"创建失败");
            }
        }
    }
    return _db;
}

+ (NSMutableArray *)selectAllNews {
    NSString *sql = [NSString stringWithFormat:@"select * from t_abc"];
    
    FMResultSet *resul = [[self shareDataBase] executeQuery:sql];
    
    NSMutableArray *mutArray = [NSMutableArray array];
    
    while ([resul next]) {
        JWAuslese *auslese = [[JWAuslese alloc] init];
        auslese.jx_id = [resul stringForColumn:@"jx_id"];
        auslese.jx_name = [resul stringForColumn:@"name"];
        auslese.jx_pic = [resul stringForColumn:@"pic"];
        auslese.jx_title = [resul stringForColumn:@"title"];
        auslese.jx_html = [resul stringForColumn:@"html"];
        auslese.jx_appkey = [resul stringForColumn:@"appkey"];
        
        
        [mutArray addObject:auslese];
    }
    
    
    return mutArray;
}

+ (NSMutableArray *)ausleseWithQueryId:(NSString *)jx_id {
    
    FMResultSet *result = [[self shareDataBase] executeQuery:@"SELECT * FROM t_abc WHERE jx_id = ?",jx_id];
    NSMutableArray *mutArray = [NSMutableArray array];
    while ([result next]) {
        JWAuslese *auslese = [[JWAuslese alloc] init];
        auslese.jx_id = [result stringForColumn:@"jx_id"];
        auslese.jx_name = [result stringForColumn:@"name"];
        auslese.jx_pic = [result stringForColumn:@"pic"];
        auslese.jx_title = [result stringForColumn:@"title"];
        auslese.jx_html = [result stringForColumn:@"html"];
        auslese.jx_appkey = [result stringForColumn:@"appkey"];
        [mutArray addObject:auslese];
    }
   
    return mutArray;
}

+ (NSMutableArray *)ausleseWithQueryAppkey:(NSString *)jx_appkey {
    
    FMResultSet *result = [[self shareDataBase] executeQuery:@"SELECT * FROM t_abc WHERE appkey = ?",jx_appkey];
    NSMutableArray *mutArray = [NSMutableArray array];
    while ([result next]) {
        JWAuslese *auslese = [[JWAuslese alloc] init];
        auslese.jx_id = [result stringForColumn:@"jx_id"];
        auslese.jx_name = [result stringForColumn:@"name"];
        auslese.jx_pic = [result stringForColumn:@"pic"];
        auslese.jx_title = [result stringForColumn:@"title"];
        auslese.jx_html = [result stringForColumn:@"html"];
        auslese.jx_appkey = [result stringForColumn:@"appkey"];
        [mutArray addObject:auslese];
    }
    
    return mutArray;
}

+ (NSMutableArray *)searchNewsWithSearchText:(NSString*)searchText {
    return 0;
}

+ (BOOL)insertNewsWithAuslese:(JWAuslese *)auslese{
//    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@(%@,%@,%@,%@,%@,%@)values(%@,%@,%@,%@,%@)",t_auslese,a_ID,name,pic,title,appkey,html,auslese.jx_id,auslese.jx_name,auslese.jx_pic,auslese.jx_title,auslese.jx_appkey];
    BOOL sussecc = [[self shareDataBase] executeUpdate:@"INSERT INTO t_abc(name,pic,title,appkey,jx_id,html)values(?, ?, ?, ?, ?, ?)",auslese.jx_name,auslese.jx_pic,auslese.jx_title,auslese.jx_appkey,auslese.jx_id,auslese.jx_html];
    if (sussecc) {
        return YES;
    }else {
        return NO;
    }
}

+ (BOOL)editNewsWithAuslese:(JWAuslese *)auslese {
    NSString *sql = [NSString stringWithFormat:@"update %@ set %@='%@',%@='%@',%@='%@',%@='%@',%@='%@' where %@ = '%@'",t_auslese,name,auslese.jx_name,pic,auslese.jx_pic,title,auslese.jx_title,appkey,auslese.jx_appkey,html,auslese.jx_html,ID,auslese.jx_id];
    BOOL sussecc =[[self shareDataBase] executeUpdate:sql];
    if (sussecc) {
        return YES;
    }else {
        return NO;
    }
}

+ (BOOL)deleteNewsWithAusleseid:(NSString *)jx_id {
    NSString *sql = [NSString stringWithFormat:@"delete from %@ where %@ = '%@'",t_auslese,ID,jx_id];
    BOOL sussecc = [[self shareDataBase] executeUpdate:sql];
    if (sussecc) {
        return YES;
    }else {
        return NO;
    }
}

@end
